<?php

class WebUser extends CWebUser {
 
    // Store model to not repeat query.
    private $_model;
 
    // Return first name.
    // access it by Yii::app()->user->first_name
    /*
  	function getFirst_Name(){
    	$user = $this->loadUser(Yii::app()->user->id);
    	return $user->first_name;
  	}
 
	// This is a function that checks the field 'role'
	// in the User model to be equal to 1, that means it's admin
	// access it by Yii::app()->user->isAdmin()
  	function isAdmin(){
    	$user = $this->loadUser(Yii::app()->user->id);
    	return intval($user->role) == 1;
  	}
  	*/
    
    public function isAdmin()
    {
        /* Session state version
        if (!isset($this->isAdmin))
            return FALSE;
        else
            return $this->isAdmin;
        */
        
        // Database real-time version
        
        if (isset($this->id))
        {
            return (boolean)Users::model()->findByPk($this->id)->isadmin;
        }
        else
        {
            return false;
        }
    }
    
    public function getIsAdmin()
    {
        return $this->isAdmin();
    }
 
    // Load user model.
    protected function loadUser($id = null)
    {
        if($this->_model === null)
        {
            if($id !== null)
                $this->_model = Users::model()->findByPk($id);
        }
        return $this->_model;
    }
}

?>